﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tormenta.Utils
{
    public class Timer
    {
        public Timer(double maxTime)
        {
            this.MaxTime = maxTime;
        }

        public double MaxTime { get; set; }

        public double ElapsedTime { get; protected set; }

        public bool Overflow { get; protected set; }

        public virtual void Update(double elapsedTime)
        {
            this.Overflow = false;
            this.ElapsedTime += elapsedTime;

            if (this.ElapsedTime > this.MaxTime)
            {
                this.Overflow = true;
                this.ElapsedTime = this.ElapsedTime - this.MaxTime;

                if (this.OnOverflow != null)
                    this.OnOverflow(this, new TimerEventArgs(this));
            }
        }
        
        public event EventHandler<TimerEventArgs> OnOverflow;
    }

    public class TimerEventArgs : EventArgs
    {
        public Timer Timer { get; private set; }

        public TimerEventArgs(Timer timer)
        {
            this.Timer = timer;
        }
    }
}
